package com.juzipi.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.juzipi.demo.mapper.DeptMapper;
import com.juzipi.demo.mapper.StudentMapper;
import com.juzipi.demo.pojo.Dept;
import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service// 类名上,声明一个业务处理类(实现非接口类)
public class StudentService {


    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DeptMapper deptMapper;


    /**
     * 根据id查询所有学生
     * @param id
     * @return
     */
    public Student queryStudentById(Integer id) {
        return this.studentMapper.selectByPrimaryKey(id);
    }



    /**
     * 新增学生
     * @param student
     * @return
     */
    @Transactional
    public void saveStudents(Student student) {
        //设置创建时间
        student.setCreateTime(new Date());
        //更新时间
        student.setUpdateTime(student.getCreateTime());
        //新增
        this.studentMapper.insertSelective(student);
    }


    /**
     * 更新学生信息
     * @param student
     * @return
     */
    @Transactional
    public void updateStudents(Student student) {
        //创建时间不更新
        student.setCreateTime(null);
        student.setUpdateTime(new Date());
        //这里是根据id更新
        this.studentMapper.updateByPrimaryKeySelective(student);
    }


    /**
     * 根据id删除学生
     * @param id
     * @return
     */
    public Integer deleteStudentById(Integer id) {
        List<Student> students = studentMapper.selectAll();
        studentMapper.deleteByPrimaryKey(id);
        return id;
    }


    /**
     * 根据条件分页查询 student
     * @param
     * @param page
     * @param rows
     * @return
     */
    public PageResult<Student> queryStudentByPage(String key, Integer page, Integer rows) {
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        //添加模糊查询或根据描述模糊查询
        if (StringUtils.isNotBlank(key)){
            criteria.andLike("name","%"+key+"%").orEqualTo("info","%"+key+"%");
        }

        //添加分页条件
        PageHelper.startPage(page,rows);

        //按示例查询
        List<Student> students = studentMapper.selectByExample(example);
        //对查询结果进行包装
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        //包装成分页结果集返回            总条数和当前页数据
        return new  PageResult<>(pageInfo.getTotal(),pageInfo.getList());

    }


    /**
     * 根据条件返回排序分页结果集
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<Student> queryStudentSortByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //初始化 example 对象
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        //根据name模糊查询，或根据描述字段
        if (StringUtils.isNotBlank(key)){
            //orEqualTo 或等于
            criteria.andLike("name","%"+key+"%").orEqualTo("info","%"+key+"%");
        }
        //添加分页条件
        PageHelper.startPage(page,rows);
        //添加排序条件
        if (StringUtils.isNotBlank(sortBy)){
            example.setOrderByClause(sortBy+ " " +(desc ? "desc" : "asc"));
        }

        List<Student> students = studentMapper.selectByExample(example);
        //包装成 studentPageInfo
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        //包装成分页结果集返回
        return new PageResult<>(studentPageInfo.getTotal(),studentPageInfo.getList());


    }


    public Map<String,Object> queryStudnetByDname(String key, Integer pageNum, Integer pageSize) {

        //逻辑有点绕了
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(key)){
            criteria.andLike("dName",key+"%");
                    /*//和不相似
                    .andNotLike("name","%"+key+"%")
                    //和大于
                    .andGreaterThan("age",20)
                    //和之间
                    .andBetween("age",20,30)
                    //和所有等于,   匹配单个字符的？
                    .andAllEqualTo(key)
                    //和条件     这个不知道
                    .andCondition(key,10)
                    //和在    这个应该好用
                    .andIn()
                    //并且不为空
                    .andIsNotNull()
                    //和小于
                    .andLessThan()
                    //和等于
                    .andEqualTo();*/
        }
        //添加分页
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptMapper.selectByExample(example);
        //上边这一块是根据 dname 模糊查询出一个 List<Dept>

        //然后根据 key 模糊查询出所有位于 dname 下的所有学生
        List<Student> studentList = studentMapper.queryStudentDept(key);
        for (Dept dept: depts) {
            //把查询到的学生添加到 dept 类中的一个额外字段 List<Student>
            dept.setStudentList(studentList);
        }
        //包装成 pageInfo
        PageInfo<Dept> pageInfo = new PageInfo<>(depts,1);//navi gate page Nums 浏览页码
        //装入map集合返回
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("条数",pageInfo.getTotal());
        hashMap.put("所有数据",pageInfo.getList());

        return hashMap;

    }


    //试试Dept表，查所有
    public List<Dept> queryDept() {

        List<Dept> depts = deptMapper.selectAll();
        return depts;
    }


    //分页结果集，用 PageResult 装
    public PageResult<Dept> queryDeptByPage(String key, Integer pageNum, Integer pageSize) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(key)){
            criteria.andLike("dName",key+"%");
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptMapper.selectByExample(example);
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);


        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());

    }
}
